package com.yy.ant.chat.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yy.ant.chat.entity.Flock;
import com.yy.ant.chat.entity.Flockrefuser;
import com.yy.ant.chat.entity.Message;
import com.yy.ant.chat.service.IFlockrefuserService;
import com.yy.ant.chat.service.IMessageService;
import com.yy.ant.sys.entity.User;
import com.yy.model.ResultMsg;
import com.yy.model.enums.ResultEnum;
import com.yy.utils.SessionUtil;

/**
 * <p>
 * 用户群组多对多 前端控制器
 * </p>
 *
 * @author liyingying
 * @since 2020-01-06
 */
@RestController
@RequestMapping("/chat/flockrefuser")
public class FlockrefuserController {

	
	@Autowired
	private IFlockrefuserService iFlockrefuserService;
	
	@Autowired
	private IMessageService iMessageService;
	/**
	 *加入群聊
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/add")
	public ResultMsg add(@RequestParam(value = "id") String id) {
		ResultMsg resultMsg=new ResultMsg();
		Message message=iMessageService.getById(id);
		Flockrefuser flockrefuser=new Flockrefuser();
		flockrefuser.setFid(message.getFromGroup());
		flockrefuser.setUid(message.getUid().longValue());
		if(isFlock(message.getUid().toString(),message.getFromGroup().toString())) {
			resultMsg.setMsg(ResultEnum.error_qun_failed.getMsg());
			return resultMsg;
		}
		boolean b=iFlockrefuserService.save(flockrefuser);
		if (b) {
			resultMsg.setSuccess(true);
			resultMsg.setMsg(ResultEnum.sucess_create_failed.getMsg());
		}else {
			resultMsg.setMsg(ResultEnum.error_create_failed.getMsg());
		}
		Message message1=new Message();
		message1.setId(Long.valueOf(id));
		message1.setTypes(3);
		message1.setReada(1);
		iMessageService.updateById(message1);
		return resultMsg;
	}
	
	/**
	 * 检查是否已经在群中
	 * 
	 * @param uid
	 * @return
	 */
	public boolean isFlock(String uid, String fid) {
		int conut=iFlockrefuserService.count(new QueryWrapper<Flockrefuser>().eq("uid", uid).eq("fid", fid));
		if (conut==0) {
			return false;
		}
		return true;
	}
}
